package com.ejd.web.vo.customer;

import java.util.ArrayList;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.controller.LoginController;
import com.ejd.service.IConsigneeService;
import com.ejd.utils.RegionUtil;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.web.bo.impl.Consignee;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.ConsigneeVo;
import com.ejd.web.vo.common.OrderPage;

@ManagedBean(name="customerNavigationPage")
@SessionScoped
public class CustomerNavigationPageBean implements java.io.Serializable{
	
	private static final long serialVersionUID = -8106629465344224728L;
	public CustomerNavigationPageBean() {
		
	}
	public String clickActionByConsigneeCustomer() {
		String result = null;
		result = (String)SpringFacesUtil.getRequestParameter("param");
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		IConsigneeService<Consignee,Long> consigneeService = (IConsigneeService<Consignee,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CONSIGNEE_SERVICE_BEAN_NAME);
		consigneeService.setEntityClass(Consignee.class);
		consigneeService.setPKClass(Long.class);
		List<Consignee> consignees = new ArrayList<Consignee>();
		consignees = consigneeService.findBy("customerId",loginCustomerPage.getCustomerInfo().getCustomerId());
		EditConsigneePageBean editConsignedPage = (EditConsigneePageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.EDIT_CONSIGNEE_PAGE_BEAN_NAME);
		List<ConsigneeVo> newConsignees= new ArrayList<ConsigneeVo>();
		BeanCopier copy = BeanCopier.create(Consignee.class, ConsigneeVo.class, false);
		if (null != consignees && consignees.size() > 0) {
			for (Consignee consigneeBo:consignees) {
				ConsigneeVo consigneeVo = new ConsigneeVo();
				copy.copy(consigneeBo, consigneeVo, null);
				newConsignees.add(consigneeVo);
			}
		}
		editConsignedPage.setDatas(newConsignees);
		editConsignedPage.setEditData(new ConsigneeVo(false));
		editConsignedPage.getErrorMessages().clear();
		editConsignedPage.getErrorMessages().add("");
		editConsignedPage.setAction(new String (EjdConstants.ACTION_NEW_TYPE));
		editConsignedPage.setProvinceItems(RegionUtil.getProvinceItems());
		editConsignedPage.setCityItems(RegionUtil.getCityItems(editConsignedPage.getEditData().getConsigneeProvince()));
		editConsignedPage.setCountyItems(RegionUtil.getCountyItems(editConsignedPage.getEditData().getConsigneeCity()));
		return (null == result)? null:result+"?faces-redirect=true";
	}
	
	public String clickActionByCustomerUpdateInfo() {
		/*System.out.println("click update info");
		String result = null;
		if (null == this.getParam() || "".equals(this.getParam())) {
			return result;
		}
		UserBean currentUser = (UserBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.CURRENT_USER_BEAN_NAME);
		if (null == currentUser || null == currentUser.getUserInfo()) {
			result = "customerLogin";
		}
		result = this.getParam();
		CustomerUpdateInfoPageBean customerUpdateInfoPage = (CustomerUpdateInfoPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.CUSTOMER_UPDATE_INFO_PAGE_BEAN_NAME);
		BeanCopier copy = BeanCopier.create(Stakeholder.class, StakeholderVo.class, false);
		StakeholderVo currStakeholder = new StakeholderVo();
		IStakeholderService stakeholderService = (IStakeholderService)SpringFacesUtil.getManagedBean(ManageBeanConstants.STAKEHOLDER_SERVICE_BEAN_NAME);
		Stakeholder theStakeholder = new Stakeholder();
		theStakeholder = stakeholderService.getStakeholderById(currentUser.getUserInfo().getId());
		currentUser.setUserInfo(theStakeholder);
		copy.copy(currentUser.getUserInfo(), currStakeholder, null);
		customerUpdateInfoPage.setStakeholder(currStakeholder);
		List<Person> persons = null;
		persons =currentUser.getUserInfo().getConatctMans();
		List<PersonVo> newPersons = new ArrayList<PersonVo>();
		if (null != persons && !(persons.isEmpty())) {
			
			for (int i = 0; i < persons.size(); i++) {
				BeanCopier copyPersons = BeanCopier.create(Person.class, PersonVo.class, false);
				Person tempPerson = new Person();
				tempPerson = persons.get(i);
				PersonVo newPerson = new PersonVo();
				copyPersons.copy(tempPerson, newPerson, null);
				newPersons.add(newPerson);
			}
		}
		customerUpdateInfoPage.setPersons(newPersons);
		EditPersonPageBean editPersonPage = (EditPersonPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.EDIT_PERSON_PAGE_BEAN_NAME);
		
		editPersonPage.setDatas(customerUpdateInfoPage.getPersons());
		PersonVo editData = new PersonVo();
		editData.setAge(18);
		editData.setSex("M");
		editData.setSelected(Boolean.TRUE);
		editPersonPage.setEditData(editData);
		editPersonPage.setSelectedData(editData);
		editPersonPage.setErrorMessages("");
		editPersonPage.setAction(new String (CommonConstants.ACTION_NEW_TYPE));
		return result;*/
		return null;
	}
	
	
	public String clickActionByConsultationCustomer() {
		String result = null;
		result = (String)SpringFacesUtil.getRequestParameter("param");
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		CustomerConsultationPageBean customerConsultationPage = (CustomerConsultationPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.CUSTOMER_CONSULTATION_PAGE_BEAN);
		customerConsultationPage.getoPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerConsultationPage.getoPage().getPageDatasForCustomer();
		customerConsultationPage.getnPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerConsultationPage.getnPage().getPageDatasForCustomer();
		customerConsultationPage.getrPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerConsultationPage.getrPage().getPageDatasForCustomer();
		return (null == result)? null:result+"?faces-redirect=true";
	}
	public String clickActionByCouponCustomer() {
		String result = null;
		result = (String)SpringFacesUtil.getRequestParameter("param");
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		CustomerCouponPageBean customerCouponPage = (CustomerCouponPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.CUSTOMER_COUPON_PAGE_BEAN);
		customerCouponPage.getoPage().getParam().setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		customerCouponPage.getoPage().getPageDatas();
		customerCouponPage.getuPage().getParam().setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		customerCouponPage.getuPage().getPageDatas();
		customerCouponPage.getaPage().getParam().setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		customerCouponPage.getaPage().getPageDatas();
		return (null == result)? null:result+"?faces-redirect=true";
	}
	
	public String clickActionByOrderCustomer() {
		String result = null;
		result = (String)SpringFacesUtil.getRequestParameter("param");
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		CustomerOrderPageBean customerOrderPage = (CustomerOrderPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.CUSTOMER_ORDER_PAGE_BEAN);
		customerOrderPage.getcPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerOrderPage.getcPage().getPageDatas();
		customerOrderPage.getpPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerOrderPage.getpPage().getPageDatas();
		return (null == result)? null:result+"?faces-redirect=true";
	}
	public String clickActionByCenterCustomer() {
		String result = null;
		result = (String)SpringFacesUtil.getRequestParameter("param");
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		CustomerCenterPageBean customerCenterPage = (CustomerCenterPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.CUSTOMER_CENTER_PAGE_BEAN);
		customerCenterPage.getnPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		List<String> status = new ArrayList<String>();
		//success(su),waiting for being reviewed(wr),prepare goods(pg),transfer(tr),waiting outstock(wo),outstock(os),refund(rf),cancel by customer(cc),cancel by user(cu),cancel by system(cs)
		status.add(EjdConstants.ORDER_STATUS_WAITING_FOR_BEING_REVIEWED);
		status.add(EjdConstants.ORDER_STATUS_PREPARE_GOODS);
		status.add(EjdConstants.ORDER_STATUS_TRANSFER);
		status.add(EjdConstants.ORDER_STATUS_WAITING_OUTSTOCK);
		status.add(EjdConstants.ORDER_STATUS_OUT_STOCK);
		status.add(EjdConstants.ORDER_STATUS_REFUND);
		status.add(EjdConstants.ORDER_STATUS_CANCEL_BY_CUSTOMER);
		status.add(EjdConstants.ORDER_STATUS_CANCEL_BY_USER);
		status.add(EjdConstants.ORDER_STATUS_CANCEL_BY_SYSTEM);
		customerCenterPage.getnPage().setStatus(status);
		customerCenterPage.getnPage().setPageNo(1);
		customerCenterPage.getnPage().setPageSize(10);
		customerCenterPage.getnPage().getParam().setCustomer(loginCustomerPage.getCustomerInfo());
		customerCenterPage.getnPage().getPageDatas();
		return (null == result)? null:result+"?faces-redirect=true";
	}

}
